package com.wimcorp.magic.client.input;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.rmi.RemoteException;
import java.util.Set;
import java.util.concurrent.Callable;

import javax.sound.midi.VoiceStatus;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.lang.StringUtils;
import org.fusesource.jansi.Ansi.Color;

import com.wimcorp.magic.client.CommandLineInterface;
import com.wimcorp.magic.exception.SystemException;
import com.wimcorp.magic.input.Target;
import com.wimcorp.magic.input.Type;
import com.wimcorp.magic.userinterface.GameInterface;

public class DeckSelector extends Selector implements Callable<Void> {

	public DeckSelector(CommandLineInterface commandLineInterface) {
		super(commandLineInterface);
		
	}

	@Override
	public Void call() {
		return selectDeck();
	}

	private Void selectDeck() {
		Options options = getSelectDeckOptions();
		
		CommandLineParser parser = new DefaultParser();
		setRun(true);
		while (isRun()) {
			getCommandLineInterface().printInstruction("Select Deck : ");
			BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
			try {
				CommandLine line = parser.parse(options, bufferRead.readLine().split(" "));
				for (Option option : line.getOptions()) {
					if ("s".equals(option.getOpt())) {
						int deckId = new Integer(option.getValue());
						getCommandLineInterface().getGame().setSelectedDeck(getCommandLineInterface().getPlayerName(), deckId);
						setRun(false);
					}
				}
				processCommands(options, line.getOptions());
			} catch (Exception e) {
				getCommandLineInterface().print(e.getMessage(), Color.RED);
			}
		}
		return null;

	}


	public Options getSelectDeckOptions() {
		Options options = new Options();
		options.addOption(getHelpOption());
		options.addOption(getListAvailableDecksOption());
		options.addOption(getPlayerListOption());
		options.addOption(getSelectOption());
		return options;
	}

}
